package com.qianmo.controller;

import com.qianmo.pojo.Result;
import com.qianmo.pojo.User;
import com.qianmo.service.UserService;
import com.qianmo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result Login(@RequestBody User user){
        User loginUser = userService.login(user);
        log.info(String.valueOf(loginUser));
        if(loginUser!=null){
            Map<String,Object> claims =new HashMap<>();
            claims.put("id",loginUser.getId());
            claims.put("username",loginUser.getUserName());
            claims.put("account",loginUser.getAccount());
            String token = JwtUtils.generateJwt(claims);
            return Result.success(loginUser,token);
        }
        return Result.error("用户名或密码错误");
    }
}
